import {
  ModalForm,
  ProFormText,
  ProFormSelect,
  ProFormTextArea,
  ProFormGroup,
} from '@ant-design/pro-form';
import { ActionType } from '@ant-design/pro-table';
import { message } from 'antd';
import { addIteration } from '@/api';
import { IterationProps } from '../../interface';

type Props = {
  record?: IterationProps;
  action: ActionType;
  children?: JSX.Element;
};

const AddInteration = (props: Props) => {
  const { children, action, record } = props;

  return (
    <ModalForm
      title="新增迭代"
      trigger={children}
      initialValues={{
        ...record,
      }}
      onFinish={async (formData) => {
        try {
          await addIteration({ ...formData });

          message.success('新增成功');
          action.reload();
          return true;
        } catch (error) {
          message.success(error.message);
        }
      }}
    >
      <ProFormText name="iterativeId" hidden />
      <ProFormGroup>
        <ProFormText
          width="lg"
          name="iterativeName"
          label="迭代名称"
          placeholder="请输入迭代名称"
          rules={[
            {
              required: true,
              message: '请输入迭代名称',
            },
          ]}
        />
      </ProFormGroup>
      <ProFormGroup>
        <ProFormSelect
          width="lg"
          name="branch"
          label="选择分支"
          placeholder="请选择迭代分支"
          valueEnum={{
            1: 'v1',
            2: 'v2',
          }}
          rules={[
            {
              required: true,
              message: '请输入迭代名称',
            },
          ]}
        />
      </ProFormGroup>
      <ProFormGroup>
        <ProFormTextArea
          width="lg"
          name="iterativeRemark"
          label="备注"
          placeholder="请输入备注"
        />
      </ProFormGroup>
    </ModalForm>
  );
};

export default AddInteration;
